﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DocumentManagement.ViewsModels;
using DocumentManagement.Models;
using System.Data;
using DocumentManagement.Views;

namespace DocumentManagement
{
    /// <summary>
    /// Interaction logic for AddDocumentOut.xaml
    /// </summary>
    public partial class AddDocumentOut : Window
    {
        AddDocumentOutViewsModel _addDocumentOutViewsModel;
        public AddDocumentOut(CreateSend send, QSHCEntities1 entity)
        {
            InitializeComponent();
            _addDocumentOutViewsModel = this.Resources["addDocumentOutViewsModel"] as AddDocumentOutViewsModel;
            _addDocumentOutViewsModel.MySend = send;
            _addDocumentOutViewsModel.EntityModels = entity;

            switch (send.Type)
            {
                case 1:
                    _addDocumentOutViewsModel.MyTypeDoc1 = true;
                    break;
                case 2:
                    _addDocumentOutViewsModel.MyTypeDoc2 = true;
                    break;
                default:
                    break;
            }


            switch (send.Speed)
            {
                case 1:
                    _addDocumentOutViewsModel.MySpeed1 = true;
                    break;
                case 2:
                    _addDocumentOutViewsModel.MySpeed2 = true;
                    break;
                case 3:
                    _addDocumentOutViewsModel.MySpeed3 = true;
                    break;
                case 4:
                    _addDocumentOutViewsModel.MySpeed4 = true;
                    break;

                default:
                    break;
            }
            switch (send.Action)
            {
                case 1:
                    _addDocumentOutViewsModel.MyAction1 = true;
                    break;
                case 2:
                    _addDocumentOutViewsModel.MyAction2 = true;
                    break;

                default:
                    break;
            }
            foreach (var item in send.ScanSendAttacheds)
            {
                _addDocumentOutViewsModel.MySend.ScanSendAttacheds.Add(item);
            }
        }

    

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            _addDocumentOutViewsModel.GetBrowse();

        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            if (StatusDocRadioButton1.IsChecked == false && StatusDocRadioButton2.IsChecked == false)
            {
                MessageBox.Show("กรุณาเลือกชนิดหนังสือ", "กรอกข้อมูลไม่ครบ");

                return;
            }
            if (DocumentCodetextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกเลขที่หนังสือ", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                DocumentCodetextBox.Focus();
                return;
            }
            if (TitletextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกชื่อเรื่อง", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                TitletextBox.Focus();
                return;
            }
            if (SpeedRadio1.IsChecked == false && SpeedRadio2.IsChecked == false && SpeedRadio3.IsChecked == false && SpeedRadio4.IsChecked == false)
            {
                MessageBox.Show("กรุณาเลือกชั้นความเร็วของหนังสือ", "กรอกข้อมูลไม่ครบ");
                return;
            }
            if (ActionRadioButton1.IsChecked == false && ActionRadioButton2.IsChecked == false)
            {
                MessageBox.Show("กรุณาเลือกการปฏิบัติของหนังสือ", "กรอกข้อมูลไม่ครบ");
                return;
            }
            //if (dataGriListFileDataGrid.Items.Count == 0)
            //{
            //    MessageBox.Show("กรุณาแนบไฟล์เอกสาร", "กรอกข้อมูลไม่ครบ");
            //    return;
            //}
          

            _addDocumentOutViewsModel.SaveToDatabase();
            PageSwitcher.Switch(new ShowItemDocumentOutUserControl());
            this.Close();
        }

        private void deleteINDocMenu_Click(object sender, RoutedEventArgs e)
        {
            _addDocumentOutViewsModel.DeleteFile();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FormDivisionIDcomboBox.SelectedValue = 17;
            FormDivisionIDcomboBox.IsEnabled = false;
            // DocumentCodetextBox.Text = "ศธ 0514.7.5.1.10" + "/" + _addDocumentOutViewsModel.GetAutoNumber; 
            if (_addDocumentOutViewsModel.MySend.EntityState == EntityState.Unchanged)
            {
                groupBox2.IsEnabled = false;
            }
                
        }

        private void StatusDocRadioButton1_Checked(object sender, RoutedEventArgs e)
        {
            //DocumentCodetextBox.Clear();
        }

        private void StatusDocRadioButton2_Checked(object sender, RoutedEventArgs e)
        {
            if (_addDocumentOutViewsModel.MySend.EntityState == EntityState.Added || _addDocumentOutViewsModel.MySend.EntityState == EntityState.Detached)
                _addDocumentOutViewsModel.GetAutoNumber();
          
                
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

     
      
    }
}
